package com.itheima.service.db.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.db.TestSoul;
import com.itheima.domain.db.TestSoulQuestion;
import com.itheima.domain.db.TestSoulQuestionsOption;
import com.itheima.mapper.TestSoulMapper;
import com.itheima.mapper.TestSoulQuestionMapper;
import com.itheima.mapper.TestSoulQuestionsOptionMapper;
import com.itheima.service.db.TestSoulService;

import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;

import java.util.List;
@DubboService
public class TestSoulServiceImpl implements TestSoulService {

    @Autowired
    private TestSoulMapper testSoulMapper;

    @Autowired
    private TestSoulQuestionMapper testSoulQuestionMapper;

    @Autowired
    private TestSoulQuestionsOptionMapper testSoulQuestionsOptionMapper;

    //问卷列表
    @Override
    public List<TestSoul> findTest(Long userId) {
        //查询问卷
        List<TestSoul> soulList = testSoulMapper.selectList(null);
        //遍历
        if (CollectionUtils.isNotEmpty(soulList)) {
            for (TestSoul testSoul : soulList) {
                LambdaQueryWrapper<TestSoulQuestion> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(TestSoulQuestion::getTestSoulId,testSoul.getId());
                List<TestSoulQuestion> questions = testSoulQuestionMapper.selectList(queryWrapper);
                if (CollectionUtils.isNotEmpty(questions)) {
                    for (TestSoulQuestion question : questions) {
                        LambdaQueryWrapper<TestSoulQuestionsOption> wrapper = new LambdaQueryWrapper<>();
                        wrapper.eq(TestSoulQuestionsOption::getTestSoulQuestionId,question.getId());
                        List<TestSoulQuestionsOption> options = testSoulQuestionsOptionMapper.selectList(wrapper);
                        if (CollectionUtils.isNotEmpty(options)) {
                            for (TestSoulQuestionsOption option : options) {
                                option.setOption(option.getOptions());
                                //option.setScore(null);
                            }
                        }
                        question.setOptions(options);
                    }
                }
                testSoul.setQuestions(questions);
            }
        }
        return soulList;
    }

    //题目列表
    @Override
    public PageBeanVo testSoulList(Integer pageNum, Integer pageSize, String name) {
        //1.创建条件
        Page<TestSoul> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<TestSoul> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotEmpty(name),TestSoul::getName,name );
        //2.查询
        page = testSoulMapper.selectPage(page, wrapper);
        //3.返回结果
        return new PageBeanVo(pageNum,pageSize,page.getTotal(),page.getRecords());
    }

    //查看问题列表
    @Override
    public PageBeanVo QuestionList(Integer pageNum, Integer pageSize, String name, Long timuId) {
        //1.创建条件
        Page<TestSoulQuestion> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<TestSoulQuestion> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotEmpty(name),TestSoulQuestion::getQuestion,name );
        wrapper.eq(TestSoulQuestion::getTestSoulId, timuId);
        //2.查询
        page = testSoulQuestionMapper.selectPage(page,wrapper );
        //3.返回结果
        return new PageBeanVo(pageNum,pageSize,page.getTotal(),page.getRecords());
    }

    //查看答案列表
    @Override
    public PageBeanVo answerList(Integer pageNum, Integer pageSize, String name, Long wentiId) {
        //1.创建条件
        Page<TestSoulQuestionsOption> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<TestSoulQuestionsOption> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotEmpty(name),TestSoulQuestionsOption::getOptions,name );
        wrapper.eq(TestSoulQuestionsOption::getTestSoulQuestionId,wentiId );
        //2.查询
        page = testSoulQuestionsOptionMapper.selectPage(page, wrapper);
        //3.返回
        return new PageBeanVo(pageNum,pageSize,page.getTotal(),page.getRecords());
    }

    //新增答案
    @Override
    public void addAnswer(TestSoulQuestionsOption testSoulQuestionsOption) {
        //1.保存至答案表中
        testSoulQuestionsOptionMapper.insert(testSoulQuestionsOption);
    }

    //修改答案
    @Override
    public void editAnswer(TestSoulQuestionsOption testSoulQuestionsOption) {
        //2.执行修改
        testSoulQuestionsOptionMapper.updateById(testSoulQuestionsOption);
    }

    //根据id删除答案
    @Override
    public void deleteAnswer(Long answerId) {
        //执行删除
        testSoulQuestionsOptionMapper.deleteById(answerId);
    }

    //根据问题id查询答案数量
    @Override
    public Integer findAnswerByQuestionId(Long questionId) {
        //1.查询该id下是否有答案
        LambdaQueryWrapper<TestSoulQuestionsOption> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TestSoulQuestionsOption::getTestSoulQuestionId,questionId );
        Integer count = testSoulQuestionsOptionMapper.selectCount(wrapper);
        return count;
    }

    //根据id删除问题
    @Override
    public void deleteQuestion(Long questionId) {
        testSoulQuestionMapper.deleteById(questionId);
    }

    //新增问题
    @Override
    public void addQuestion(TestSoulQuestion testSoulQuestion) {
        testSoulQuestionMapper.insert(testSoulQuestion);
    }

    //新增题目
    @Override
    public void addTest(TestSoul testSoul) {
        testSoulMapper.insert(testSoul);
    }

    //查询题目下问题数量
    @Override
    public Integer findQuestionByTestId(Long testId) {
        //1.创建条件
        LambdaQueryWrapper<TestSoulQuestion> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TestSoulQuestion::getTestSoulId,testId );
        //2.查询数量
        Integer count = testSoulQuestionMapper.selectCount(wrapper);
        return count;
    }

    //删除题目
    @Override
    public void deleteTest(Long testId) {
        testSoulMapper.deleteById(testId);
    }
}
